
var socket = io();

$(".exit").click(function () {
    socket.emit("disconnect1", { uname: $('.uname').html() });//告诉服务器用户退出聊天室
    // console.log($('.uname').html());
    setTimeout(function () {
        location.href = '/';
    }, 1000)
});


$(".send").click(function () {
    if ($('input').val().trim() == '') {
        swal('发送内容不能为空');
        return false;
    }
    socket.emit("chat", {
        info: $('input').val(),
        uname: $('.uname').html()
    });
    $('input').val("");
});

socket.emit("login", { uname: $('.uname').html() });//告诉服务器用户进入聊天室

socket.on("send", (msg) => {
    $(".writeFarming").append($(`<li>${msg.uname} : ${msg.info}</li>`));
    var h = ($("ul").height()) / ($("li").height());
    if ($("ul").children("li").length > h) {
        var num = $("ul").children("li").length - h;
        $(".writeFarming").scrollTop($("li").height() * num);
    }
});


//用户加入聊天室
socket.on("login", (msg) => {
    $(".writeFarming").append($(`<li>系统消息 : 用户 ${msg.uname} 闪亮登场</li>`));
    $(".userNum").html(msg.num);
    $(".users").html("");
    msg.user.forEach((ele) => {
        $(".users").append($(`<li>${ele}</li>`));
    });

    console.log(msg.user);
});
//用户退出聊天室
socket.on("disconnect2", (msg) => {
    $(".writeFarming").append($(`<li>系统消息 : 用户 ${msg.uname} 离开了聊天室</li>`));
    $(".userNum").html(msg.num);
    $(".users").html("");
    msg.user.forEach((ele) => {
        $(".users").append($(`<li>${ele}</li>`));
    });
});

//音乐

$(document).ready(function () {
    var audioElement = document.createElement('audio');
    audioElement.setAttribute('src', $('.active-song').attr('data-src'));

    var tl = new TimelineMax();
    tl.to('.player__albumImg', 3, {
        rotation: '360deg',
        repeat: -1,
        ease: Power0.easeNone
    }, '-=0.2');
    tl.pause();

    $('.player__play').click(function () {

        if ($('.player').hasClass('play')) {
            $('.player').removeClass('play');
            audioElement.pause();
            TweenMax.to('.player__albumImg', 0.2, {
                scale: 1,
                ease: Power0.easeNone
            })
            tl.pause();
        } else {
            $('.player').addClass('play');
            audioElement.play();
            TweenMax.to('.player__albumImg', 0.2, {
                scale: 1.1,
                ease: Power0.easeNone
            })
            tl.resume();
        }

    });


    var playhead = document.getElementById("playhead");
    audioElement.addEventListener("timeupdate", function () {
        var duration = this.duration;
        var currentTime = this.currentTime;
        var percentage = (currentTime / duration) * 100;
        playhead.style.width = percentage * 4 + 'px';
    });

    function updateInfo() {
        $('.player__author').text($('.active-song').attr('data-author'));
        $('.player__song').text($('.active-song').attr('data-song'));
    }
    updateInfo();

    $('.player__next').click(function () {
        if ($('.player .player__albumImg.active-song').is(':last-child')) {
            $('.player__albumImg.active-song').removeClass('active-song');
            $('.player .player__albumImg:first-child').addClass('active-song');
            audioElement.addEventListener("timeupdate", function () {
                var duration = this.duration;
                var currentTime = this.currentTime;
                var percentage = (currentTime / duration) * 100;
                playhead.style.width = percentage * 4 + 'px';
            });
        } else {
            $('.player__albumImg.active-song').removeClass('active-song').next().addClass('active-song');
            audioElement.addEventListener("timeupdate", function () {
                var duration = this.duration;
                var currentTime = this.currentTime;
                var percentage = (currentTime / duration) * 100;
                playhead.style.width = percentage + '%';
            });
        }
        updateInfo();
        audioElement.setAttribute('src', $('.active-song').attr('data-src'));
        audioElement.play();
    });

    $('.player__prev').click(function () {
        if ($('.player .player__albumImg.active-song').is(':first-child')) {
            $('.player__albumImg.active-song').removeClass('active-song');
            $('.player .player__albumImg:last-child').addClass('active-song');
            audioElement.addEventListener("timeupdate", function () {
                var duration = this.duration;
                var currentTime = this.currentTime;
                var percentage = (currentTime / duration) * 100;
                playhead.style.width = percentage * 4 + 'px';
            });
        } else {
            $('.player__albumImg.active-song').removeClass('active-song').prev().addClass('active-song');
            audioElement.addEventListener("timeupdate", function () {
                var duration = this.duration;
                var currentTime = this.currentTime;
                var percentage = (currentTime / duration) * 100;
                playhead.style.width = percentage + 'px';
            });
        }
        updateInfo();
        audioElement.setAttribute('src', $('.active-song').attr('data-src'));
        audioElement.play();
    });

});
